package com.douhao.personal.core.shiro.token.manager;

import com.douhao.personal.core.shiro.token.ShiroToken;
import com.douhao.personal.model.User;
import org.apache.shiro.SecurityUtils;

public class TokenManager {

    /**
     * 获取当前登录的用户User对象
     * @return
     */
    public static User getToken(){
        User token = (User) SecurityUtils.getSubject().getPrincipal();
        return token;
    }

    /**
     * 获取当前用户ID
     * @return
     */
    public static String getUserId(){
        return getToken()==null?null:String.valueOf(getToken().getId());
    }

    /**
     * 登录
     * @param user
     * @param remberMe
     * @return
     */
    public static User login(User user,boolean remberMe){
        ShiroToken shiroToken = new ShiroToken(user.getEmail(), user.getPswd());
        shiroToken.setRememberMe(remberMe);
        SecurityUtils.getSubject().login(shiroToken);
        return getToken();
    }

    /**
     * 退出登录
     */
    public static void logout() {
        SecurityUtils.getSubject().logout();
    }
}
